<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-23
 * Time: 下午5:41
 */
class ShopcartController extends Controller {

    private $_daytime;
    private $_user_info;

    public function init() {
        parent::init();

        $this->_user_info = $this->_session->offsetGet('loginInfo');
        $__now_time = $_SERVER['REQUEST_TIME'];
        $this->_daytime = date("Y-m-d H:i:s",$__now_time);
        $this->_daydate = date("Y-m-d",$__now_time);
    }

    //立即认购
    public function indexAction(){
        global $_saleOrderType, $_saleOrderStatus, $_SupplyOrderCfg;
        $this->_title ="确认订单";
        $this->_view->title = $this->_title;
        //先判断是否登录
        if(!$this->_session->has('loginInfo')) {
            $this->redirect('/login');
            die();
        }
        $this->_logininfo = $this->_session->offsetGet('loginInfo');


        //读取订单ID值及下单类型，第一种自营，第二种第三方
        $qid = $this->_request->getParam('qid','0');
        $type = $this->_request->getParam('type','price');
        $qid = (int)$qid;

        if(empty($qid)){
            $this->redirect('/index');
            die;
        }

        //取出该用户的行为规则
        $ua = new UseractionModel();
        $user_action ='';
        $uawhere = "user_id={$this->_logininfo['id']}";
        $user_action = $ua->getRowByWhere($uawhere);
        $this->_view->user_action = $user_action;

        //取出公司信息，如果没有的需要进行维护并保存
        $cp = new UsersinvoiceModel();
        $conpanylist ='';
        $contacts = '';
        $business_id ='';
        $conpanylist = $cp->getListByRow('user_id',"{$this->_logininfo['id']}",'id');

        if(!empty($conpanylist)) {
            foreach ($conpanylist as $var) {
                if ($var['is_default'] == 'Y') {
                    $contacts['business_name'] = $var['business_name'];
                    $contacts['contacts'] = $var['contacts'];
                    $contacts['mobile'] = $var['mobile'];
                    $business_id = $var['id'];
                }
            }

        }

        $this->_view->conpanylist = $conpanylist;

        if(!$this->_session->has('orderinfo')) {
            $this->_session->offsetSet('orderinfo',array());
        }

        //这里需要仔细分别用户id 公司id
        if($type == 'price'){
            //开始自有现货
            $validate =array(
                'id'  =>  $qid ,
            );

            $url = _HTTP_CLOUD_HOST."/api/getprice/getQuoteById";
            $prodcut = Tools::https_request($url,$validate);
            $getlist = json_decode($prodcut,true);
            $product = $getlist['data'];

            $order_info = array(

            );
            //取产品ID值
            $p = new ProductModel();
            $where = "product_name like '{$getlist['data']['product_name']}' and brand_name like '{$getlist['data']['brand_name']}' and cate_name like '{$getlist['data']['cate_name']}'";
            $product_id = $p->getProductIdByWhere($where);

            //开始进行SESSION数据处理
            $order_info = array(
                'order_soft' => 0,
                'create_uid' => $this->_logininfo['id'],//创建人
                'create_time' => $this->_daytime,// 创建时间
                'update_time' => $this->_daytime,// 更新时间
                'update_uid' => $this->_logininfo['id'],// 更新人
                'business_id' => $business_id,
                'order_key' => '0',// 订单编号
                'user_id' => $this->_logininfo['id'], // 客户ID
                'order_from' =>  'mobile', // 订单来源
                'order_type' => $_saleOrderType['Own'], // 订单类型
                'order_date' => $this->_daytime, //创建日期
                'status' => $_SupplyOrderCfg['audit'],// 订单状态
                'organization_id' => '1',// 事业部ID
                'worker_id' => '0',// 管理员ID
                'department_id' => '0', // 部门ID
                'order_amount' => (5*$getlist['data']['price']),// 总金额
                'is_join' => 'N',// 是否对接
                'contract_no' => '0',// 合同编号
                'pay_delay' => 'N',// 延长付款
                'pay_join' => 'N',// 是否支付对接
                'pay_type' => '1', // 支付类型
                'deposit_amount' => '0', //
                'is_paid' => 'N',// 是否已经支付
                'checked_time' => '0', // 订单审核通过日期
                'remark' => '0', //
                'pay_amount' => '0',// 已支付的金额
                'price_info' => $getlist,
                'business_name'  =>"上海塑米信息科技有限公司",
                'contacts' => $getlist['data']['realname'],
                'mobile' => $getlist['data']['mobile'],
                'qq' => $getlist['data']['qq'],
                'order_sku' => array(
                    0 => array(
                        'create_uid' => $this->_logininfo['id'],// 创建人
                        'create_time' =>$this->_daytime,// 创建时间
                        'update_time' =>$this->_daytime,// 更新时间
                        'update_uid' =>$this->_logininfo['id'],// 更新人
                        'order_id' =>0,// 订单ID
                        'product_id' =>$product_id,// 产品ID
                        'product_name' => $getlist['data']['product_name'],
                        'brand_name' => $getlist['data']['brand_name'],
                        'cate_name' => $getlist['data']['cate_name'],
                        'delivery_area' => $getlist['data']['delivery_area'], // 交货地点
                        'delivery_time' => $getlist['data']['delivery_time'], // 交货时间
                        'service_id' =>$getlist['data']['service_id'],// 产品类型
                        'service_name' =>$getlist['data']['service_name'],// 产品类型
                        'currency_id' =>$getlist['data']['currency_id'],// 货币
                        'uom_id' =>'1', // 主单位
                        'tax_rate' =>$getlist['data']['deposit_rate'], // 税率
                        'vice_uom_id' =>'0',// 副单位
                        'uom_rate' =>'0',// 单位率
                        'num' => 5,// 数量
                        'price' =>$getlist['data']['price'], // 单价
                        'currency_mark' => $getlist['data']['currency_mark'],
                        'settlement'    =>  $getlist['data']['settlement'],
                        'amount' =>(5*$getlist['data']['price']),// 小计
                        'buy_sku_type' =>1,// 报价采购SKU_ID
                        'buy_sku_id' =>$getlist['data']['iqr_id'],// 报价认购SKU_ID
                        'purchase_sku_id' =>0, // 采购单ID
                        'deposit' =>$getlist['data']['deposit_rate'],// 保证金
                        'delivery_method' =>$getlist['data']['delivery_method'],// 提货方式（1:自提 2：配送）
                        'delivery_addr' =>$getlist['data']['warehouse_addr'],// 配送地址
                        'remark' =>$getlist['data']['remark'],// 备注
                        'requested_date' =>'0', // 要求的交货时间
                        'freight_type' =>'0', //
                        'freight' =>'0',//
                        'is_join' =>'N',// 是否已对接K3
                        'is_pay_join' =>'N',//
                        'return_num' =>0,//
                        'real_num' => $getlist['data']['remain'],//
                        'inout_num' =>0,// 已出库数量
                        'tray_type' =>0,// 托盘类型
                        'delivery_num' =>0,//
                    )
                )
            );

        }else{
            $quota = new  QuotationModel();
            $product = $quota->getDetailsById("iq_quotation.id =".$qid);

            //开始进行SESSION数据处理
            $order_info = array(
                'order_soft' => 1,
                'create_uid' => $this->_logininfo['id'],//创建人
                'create_time' => $this->_daytime,// 创建时间
                'update_time' => $this->_daytime,// 更新时间
                'update_uid' => $this->_logininfo['id'],// 更新人
                'business_id' => $business_id,//订单选择默认公司
                'order_key' => '0',// 订单编号
                'user_id' => $this->_logininfo['id'], // 客户ID
                'order_from' =>  'mobile', // 订单来源
                'order_type' => $_saleOrderType['Third'], // 订单类型
                'order_date' => $this->_daytime, //创建日期
                'status' => $_SupplyOrderCfg['audit'],// 订单状态
                'organization_id' => '1',// 事业部ID
                'worker_id' => '0',// 管理员ID
                'department_id' => '0', // 部门ID
                'order_amount' => (5*$product['price']),// 总金额
                'is_join' => 'N',// 是否对接
                'inquiry_id' => $product['inquiry_id'],
                'contract_no' => '0',// 合同编号
                'pay_delay' => 'N',// 延长付款
                'pay_join' => 'N',// 是否支付对接
                'pay_type' => '1', // 支付类型
                'deposit_amount' => '0', //
                'is_paid' => 'N',// 是否已经支付
                'checked_time' => '0', // 订单审核通过日期
                'remark' => '0', //
                'pay_amount' => '0',// 已支付的金额
                'price_info' => $product,
                'business_name'  => empty($contacts['business_name']) ? '' :$contacts['business_name'],
                'contacts' => $product['realname'],
                'mobile' => $product['mobile'],
                'qq' => $product['qq'],
                'order_sku' => array(
                    0 => array(
                        'create_uid' => $this->_logininfo['id'],// 创建人
                        'create_time' =>$this->_daytime,// 创建时间
                        'update_time' =>$this->_daytime,// 更新时间
                        'update_uid' =>$this->_logininfo['id'],// 更新人
                        'order_id' =>0,// 订单ID
                        'product_id' =>$product['product_id'],// 产品ID
                        'product_name' => $product['product_name'],
                        'brand_name' => $product['brand_name'],
                        'cate_name' => $product['cate_name'],
                        'delivery_area' => $product['delivery_area'], // 交货地点
                        'delivery_time' => empty($product['delivery_time']) ? 0 :$product['delivery_time'], // 交货时间
                        'service_id' =>empty($product['service_id'])? 0 :$product['service_id'],// 产品类型
                        'service_name' =>$product['service_name'],// 产品类型
                        'currency_id' =>$product['currency_id'],// 货币
                        'uom_id' =>'1', // 主单位
                        'tax_rate' =>$product['deposit_rate'], // 税率
                        'vice_uom_id' =>'0',// 副单位
                        'uom_rate' =>'0',// 单位率
                        'num' => 5,// 数量
                        'price' =>$product['price'], // 单价
                        'currency_mark' => $product['currency_mark'],
                        'settlement'    =>  $product['settlement'],
                        'amount' =>(5*$product['price']),// 小计
                        'buy_sku_type' =>2,// 报价采购SKU_ID
                        'buy_sku_id' =>$product['id'],// 报价认购SKU_ID
                        'purchase_sku_id' =>0, // 采购单ID
                        'deposit' =>$product['deposit_rate'],// 保证金
                        'delivery_method' =>$product['delivery_method'],// 提货方式（1:自提 2：配送）
                        'delivery_addr' =>$product['warehouse_addr'],// 配送地址
                        'remark' =>$product['remark'],// 备注
                        'requested_date' =>'0', // 要求的交货时间
                        'freight_type' =>'0', //
                        'freight' =>'0',//
                        'is_join' =>'N',// 是否已对接K3
                        'is_pay_join' =>'N',//
                        'return_num' =>0,//
                        'real_num' => $product['num'],//
                        'inout_num' =>0,// 已出库数量
                        'tray_type' =>0,// 托盘类型
                        'delivery_num' =>0,//
                    )
                )
            );
        }


        $this->_session->offsetSet('orderinfo',$order_info);


        $this->_view->order_info = $order_info;
        $this->_view->contacts = $contacts;
        $this->_view->product = $product;
    }

    //创建订单
    public function createorderAction(){

        $orderinfo = $this->_session->offsetGet('orderinfo');
        global $_SmsSendSwitch, $_saleOrderType, $_salePayTypeTextcfg, $_saleOrderSourcecfg, $_BuyStateCfg, $_BidPurchaseCfg, $_SmsSendContent;

        if(empty($orderinfo)){
            $ret = array(
                'code'  => 404,
                'msg'   => '',
                'data'  => array()
            );
            $this->redirect('/index');
        }
        //读出买入的公司
        if(!empty($orderinfo['business_id'])){
            $ui = new UsersinvoiceModel();
            $buyuserinfo = $ui->getlineByRow("id",$orderinfo['business_id']);
        }

        $order = new SaleorderModel();

        try {
            $orderdata = array(
                'create_uid'  => $orderinfo['create_uid'],//创建人
                'create_time' => $orderinfo['create_time'],// 创建时间
                'update_time' => $orderinfo['update_time'],// 更新时间
                'update_uid'  => $orderinfo['update_uid'],// 更新人
                'order_key'   => new Zend_Db_Expr('generateOrderKeyStr()'), // 订单编号
                'user_id'     => $orderinfo['user_id'], // 客户ID
                'business_id' => $orderinfo['business_id'],// 店铺ID
                'order_from'  => $orderinfo['order_from'], // 订单来源
                'order_type'  => $orderinfo['order_type'], // 订单类型
                'order_date'  => $orderinfo['order_date'], //创建日期
                'status'      => $orderinfo['status'],// 订单状态
                'organization_id' => $orderinfo['organization_id'],// 事业部ID
                'worker_id'   => $orderinfo['worker_id'],// 管理员ID
                'department_id'=> $orderinfo['department_id'], // 部门ID
                'order_amount'=> $orderinfo['order_amount'],// 总金额
                'is_join'     => $orderinfo['is_join'] ,// 是否对接
                'contract_no' => $orderinfo['contract_no'] ,// 合同编号
                'pay_delay'   => $orderinfo['pay_delay'],// 延长付款
                'pay_join'    => $orderinfo['pay_join'],// 是否支付对接
                'pay_type' => $orderinfo['pay_type'], // 支付类型
                'deposit_amount' => $orderinfo['deposit_amount'], //
                'is_paid' => $orderinfo['is_paid'],// 是否已经支付
                'checked_time' => $orderinfo['checked_time'], // 订单审核通过日期
                'remark' => $orderinfo['remark'], //
                'pay_amount' => $orderinfo['pay_amount'],// 已支付的金额
            );
            $orderID = $order->add($orderdata);


            if(empty($orderID)){
                throw new Exception('创建订单失败！');
            }else{
                $sku = new SaleorderskuModel();
                if(!empty($orderinfo['order_sku'])){
                    foreach($orderinfo['order_sku'] as $var){
                        $saleordersku=array(
                            'create_uid' =>$var['create_uid'],
                            'create_time' =>$var['create_time'],
                            'update_time' =>$var['update_time'],
                            'update_uid' =>$var['update_uid'],
                            'order_id' =>$orderID,
                            'product_id' =>$var['product_id'],
                            'delivery_area'=>$var['delivery_area'],
                            'delivery_time'=>$var['delivery_time'],
                            'service_id'=>$var['service_id'],
                            'currency_id'=>$var['currency_id'],
                            'uom_id'=>$var['uom_id'],
                            'tax_rate'=>$var['tax_rate'],
                            'vice_uom_id'=>$var['vice_uom_id'],
                            'uom_rate'=>$var['uom_rate'],
                            'num'=>$var['num'],
                            'price'=>$var['price'],
                            'amount'=>$var['amount'],
                            'buy_sku_type'=>$var['buy_sku_type'],
                            'buy_sku_id'=>$var['buy_sku_id'],
                            'purchase_sku_id'=>$var['purchase_sku_id'],
                            'deposit'=>$var['deposit'],
                            'delivery_method'=>($var['delivery_method']=='T') ? 1 : 2,
                            'delivery_addr'=>$var['delivery_addr'],
                            'remark'=>$var['remark'],
                            'requested_date'=>$var['requested_date'],
                            'freight_type'=>$var['freight_type'],
                            'freight'=>$var['freight'],
                            'is_join'=>$var['is_join'],
                            'is_pay_join'=>$var['is_pay_join'],
                            'return_num'=>$var['return_num'],
                            'real_num'=>$var['real_num'],
                            'inout_num'=>$var['inout_num'],
                            'tray_type'=>$var['tray_type'],
                            'delivery_num'=>$var['delivery_num'],
                        );

                        $affactRows = $sku->add($saleordersku);
                        if($affactRows < 1) throw new Exception('创建订单SKU表失败！');

                        //更新扣除库存
                        if($orderinfo['order_type'] == $_saleOrderType['Third']){
                            $order->getDB()->query("update iq_quotation set num = num-{$var['num']} where id='{$orderinfo['price_info']['id']}'");
                            //更改第三方状态包括求购下单的
                            if($orderinfo['order_soft'] == 1){
                                $iqi = new BuyModel();
                                $iqi->updateById("id = '{$orderinfo['inquiry_id']}'",array('update_time' =>$this->_daytime ,'status' => $_BuyStateCfg['Trading']));

                                $iqt = new QuotationModel();
                                $iqt->updateById("id='{$orderinfo['order_sku']['buy_sku_id']}'",array('update_time' =>$this->_daytime ,'status' => $_BidPurchaseCfg['deal']));
                            }
                        }else{
                            foreach($orderinfo['order_sku'] as $var){
                                $validate = array(
                                    'pay_type' => $_salePayTypeTextcfg['cod'],
                                    'user_id'  => $orderinfo['user_id'],
                                    'order_from' => $_saleOrderSourcecfg['m'],
                                    'quote_id[]' => $orderinfo['price_info']['data']['iqr_id'],
                                    'num[]' => $var['num'],
                                    'delivery_method[]' => $orderinfo['price_info']['data']['delivery_method'],
                                    'delivery_addr[]' => empty($orderinfo['delivery_area']) ? "无" : $orderinfo['delivery_area'],
                                    'business_name'   => $buyuserinfo['business_name'],
                                    'worker_id'  => 0,
                                    'username'  => $buyuserinfo['contacts'],
                                    'mobile' => $buyuserinfo['mobile'],
                                );
                                $url = _HTTP_CLOUD_HOST."/api/buy/create";
                                $order = Tools::https_request($url,$validate);
                                $order_info = json_decode($order,true);
                                if($order_info['status']  != 'success')  throw new Exception('创建订单失败！');
                            }
                        }
                    }
                }
            }

            //开启下订单发送短信.这里要注意的是只有库存下单，与第三方下订单。
            if($_SmsSendSwitch['op_cod'] === 1){
                if($_SESSION['order_info']['order_type'] == $_saleOrderType['Third']){
                    $product = $orderinfo['price_info']['cate_name'] ." ". $orderinfo['price_info']['product_name']  ." ". $orderinfo['price_info']['brand_name'];
                    $phone = $_SESSION['order_info']['mobile'];
                    $sms = new sendSmsJxt($this->_db_handler,null);
                    $content = str_replace('%d1',$product,$_SmsSendContent['op_cod_c']);
                    $sms->otherSend($phone,$content);
                }
            }

            unset($_SESSION['orderinfo']);
            $this->redirect('/shopcart/success/orderid/'.$orderID);
            die();
        }catch (Exception $e) {
            $this->redirect('/index');
            die();
        }

    }

    //认购成功
    //todo 这里有个问题就是清楚SESSION
    public function successAction(){
        $orderid = $this->_request->getParam('orderid');

        $this->_title ="提交成功";
        $this->_view->title = $this->_title;

//        $orderinfo = $this->_session->offsetGet('orderinfo');
        if(empty($orderid)){
            $this->redirect('/index');
            die;
        }

        $order = new SaleorderModel();
        $orderinfo = $order->getsaleorder("sale_order.id='{$orderid}'");

        if($orderinfo['buy_sku_type'] == 1) {
            $validate = array(
                'id' => $orderinfo['buy_sku_id'],
            );

            $url = _HTTP_CLOUD_HOST . "/api/getprice/getQuoteById";
            $prodcut = Tools::https_request($url, $validate);
            $getlist = json_decode($prodcut, true);
            $productinfo = $getlist['data'];

            $orderinfo['business_name'] = $productinfo['short_name'];
            $orderinfo['contacts'] = $productinfo['realname'];
            $orderinfo['mobile'] = $productinfo['mobile'];
            $orderinfo['qq'] = $productinfo['qq'];
        }else{
            $quota = new QuotationModel();
            $quotainfo = $quota->getListById("iq_quotation.id='{$orderinfo['buy_sku_id']}'");

            $orderinfo['business_name'] = $quotainfo['business_name'];
            $orderinfo['contacts'] = $quotainfo['contacts'];
            $orderinfo['mobile'] = $quotainfo['mobile'];
            $orderinfo['qq'] = $quotainfo['qq'];
        }

        $this->_view->orderinfo = $orderinfo;

    }

    //更新配送方式
    public function deliveryAction(){
        if($this->_request->isPost()){
            $Post = $this->_request->getPost();

            $Post['dm'] = empty($Post['dm']) ? 0 : $Post['dm'];
            $Post['dr']  =  empty($Post['dr']) ? 0 : $Post['dr'];
            if(empty($this->_user_info['id'])){
                die(2);
            }

            //取出该用户的行为规则
            $ua = new UseractionModel();
            $uawhere = "user_id={$this->_user_info['id']}";
            $user_action = $ua->getRowByWhere($uawhere);


            //开始处理
            $action =array();
            if(!empty($user_action)){
                $action =array(
                    'area_id' => $_POST['dm'],
                );

                $updata_action = $ua->updateByWhere("user_id='{$this->_user_info['id']}'",$action);

                if(!empty($updata_action)){
                    echo '8';
                }else{
                    die(2);
                }

            }else{
                $action =array(
                    'user_id' => $this->_user_info['id'],
                    'area_id' => $Post['dm'],
                );
                $updata_action = $ua->add($action);
                if(!empty($updata_action)){
                    echo '8';
                }else{
                    die(2);
                }
            }

            //开始设置session
            $orderinfo = $this->_session->offsetGet('orderinfo');
            $orderinfo['delivery_method'] = $Post['dm'];
            $orderinfo['delivery_addr']= $Post['dr'];
            $v = $this->_session->offsetSet('orderinfo',$orderinfo);
        }
        die();
    }

    public function delivery_addrAction(){
        if($this->_request->isPost()){
            $Post = $this->_request->getPost();
            $Post['dr']  =  empty($Post['dr']) ? 0 : htmlspecialchars(trim($Post['dr']), ENT_QUOTES, 'utf-8');

            $orderinfo = $this->_session->offsetGet('orderinfo');

            $orderinfo['order_sku'][0]['delivery_addr'] = $Post['dr'];
            $this->_session->offsetSet('orderinfo',$orderinfo);
            $orderinfo = $this->_session->offsetGet('orderinfo');
            echo json_encode($orderinfo);
            die();
        }
        die();
    }


    public function updatenumAction(){
        if($this->_request->isPost()){
            $Post = $this->_request->getPost();

            $Post['product_num'] = empty($Post['product_num']) ?  '' : trim($Post['product_num']);

            $orderinfo = $this->_session->offsetGet('orderinfo');
            $orderinfo['order_sku'][0]['num'] = $Post['product_num'];
            $v = $this->_session->offsetSet('orderinfo',$orderinfo);
            echo json_encode($v);
            die();
        }
        die();
    }


    public function user_invoiceAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['select_id'] = empty($Post['select_id']) ? 0 : (int) $Post['select_id'];
            if(empty($Post['select_id'])){
                return;
            }

            $orderinfo = $this->_session->offsetGet('orderinfo');
            $orderinfo['business_id']= $Post['select_id'];
            $this->_session->offsetSet('orderinfo',$orderinfo);

            $data = array(
                'code' => '',
                'msg' =>''
            );

            $ui = new UsersinvoiceModel();
            $ret = $ui->getRowByWhere("id='{$Post['select_id']}'");
            if(empty($ret)){
                $data =array(
                    'code' => '7',
                    'msg' =>'无法获取公司信息'
                );
                echo json_encode($data);
                die();
            }else{
                $data =array(
                    'code' => '8',
                    'msg' =>'公司信息',
                    'data' =>$ret
                );
                echo json_encode($data);
                die();
            }
        }
    }


    public function addinvoiceAction(){
        if($this->_request->isPost()) {
            $Post = $this->_request->getPost();
            $Post['business_name']  =  empty($Post['business_name']) ? 0 : htmlspecialchars(trim($Post['business_name']), ENT_QUOTES, 'utf-8');;
            $Post['contacts']  =  empty($Post['contacts']) ? 0 : htmlspecialchars(trim($Post['contacts']), ENT_QUOTES, 'utf-8');;
            $Post['mobile']  =  empty($Post['mobile']) ? 0 : htmlspecialchars(trim($Post['mobile']), ENT_QUOTES, 'utf-8');;
            $retdata = array(
                'code' => '',
                'msg' =>''
            );

            $data = array(
                'user_id' =>  $this->_user_info['id'],
                'invoice_type'=> 'Z',
                'title' => $Post['business_name'],
                'business_name' => $Post['business_name'],
                'business_remarks' => $Post['business_name'],
                'business_phone' => $Post['mobile'],
                'contacts' => $Post['contacts'],
                'mobile' => $Post['mobile'],
                'is_default' => 'Y',
                'visit' => '1'
            );

            $userinvoice = new UsersinvoiceModel();
            $ise = $userinvoice->getRowByWhere("business_name = '{$Post['business_name']}'");
            if(!empty($ise)){
                $retdata = array(
                    'code' => '7',
                    'msg' =>'已经公司存在！',
                );
                echo json_encode($retdata);
                die();

            }else{
                $ret = $userinvoice->add($data);
                if(!empty($ret)){
                    $orderinfo = $this->_session->offsetGet('orderinfo');
                    $orderinfo['business_id'] = $ret;
                    $this->_session->offsetSet('orderinfo',$orderinfo);
                }
            }

            $retdata = array(
                'code' => '8',
                'msg' =>'添加成功！',
                'invoiceid' => empty($ise['id']) ? $ret :$ise['id'],
            );
            echo json_encode($retdata);
            die();
        }
        die();
    }
}